//
//  APPLEStatusViewModel.swift
//  123
//
//  Created by pangjiaxing on 16/1/2.
//  Copyright © 2016年 pangjiaxing. All rights reserved.
//

import UIKit

class APPLEStatusViewModel: NSObject {
    
    // 会员显示的图片
    var memberImage: UIImage?
    // 认证图片
    var verifiedImage: UIImage?
    
    // 转发评论赞的数量字符串
    var repostsCount: String?
    var commentsCount: String?
    var attitudesCount: String?
    
    /// 转发微博的内容(如果有转发微博的话，这个属性才有意义)
    var retweetStatusText: String? // @昵称: 转发微博的内容
    
    
    
    
    
    // 当前微博的配图数组（这个配图可能是原创的微博，也可能是转发微博）
    var picUrls:[APPLEStatusPictureInfo]?
    
    
    
    
    var status:APPLEStatus?{
        didSet{
            if let mbrank = status?.user?.mbrank where mbrank > 0 && mbrank<7 {
                memberImage=UIImage(named: "common_icon_membership_level\(mbrank)")
            }else{
                memberImage=nil
            }
            
           verifiedImage = calcVerifiedImage()
            
           repostsCount = calcStatusCount(status?.reposts_count ?? 0, defalutTitle: "转发")
           commentsCount = calcStatusCount(status?.comments_count ?? 0, defalutTitle: "评论")
           attitudesCount = calcStatusCount(status?.attitudes_count ?? 0,defalutTitle: "赞")
           
            if let retweetText = status?.retweeted_status?.text{
                if let name = status?.retweeted_status?.user?.screen_name{
                    retweetStatusText = "@\(name):\(retweetText)"
                }
            }
            
            if let urls = status?.pic_urls where urls.count>0 {
                picUrls = urls
            }else if let urls = status?.retweeted_status?.pic_urls where urls.count>0 {
                picUrls = urls
            }
   
           
            dealSourceText()
            
            creatSourceText()
        }
    }


    private func calcStatusCount(count:Int,defalutTitle:String) -> String {
    if count == 0 {
         return defalutTitle
    }
        if count < 10000 {
            return("\(count)")
        }else{
            let result = CGFloat(count/1000)/10
            let resultStr = "\(result)万"
            if resultStr.containsString(".0"){
                return resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            return resultStr
        }
    }
    
    private func calcVerifiedImage() -> UIImage? {
        if let verifiedType = status?.user?.verified_type{
            switch verifiedType {
            case 0:
                return UIImage(named: "avatar_vip")
            case 2,3,5:
                return UIImage(named: "avatar_enterprise_vip")
            case 220:
                return UIImage(named: "avatar_grassroot")
            default:
                return nil
            }
        }
        return nil
    }
    
    
    var sourceText:String?
    
    private func dealSourceText(){
        guard let text = status?.source else{
            return
        }
        if let preRange = text.rangeOfString("\">"){
            if let sufRange = text.rangeOfString( "</" ){
            sourceText="来自\(text.substringWithRange(preRange.endIndex..<sufRange.startIndex))"
            }
        }
    }
    // 创建时间
    var createText: String?
    
    private func creatSourceText(){
        createText = NSDate.sinaDate(status?.created_at)?.sinaDateDescription
    }
}
